//
//  HydroRepairController.m
//  PropertyMananger
//
//  Created by bh on 17/1/16.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "HydroRepairController.h"
#import "HydroRepairCell.h"
#import "HydroRepairDetailController.h"
#import "PublicRepairModel.h"

@interface HydroRepairController ()

@property (nonatomic,strong) NSArray * iconArr;//icon数据

@property (nonatomic,strong) NSMutableArray * eletricsArray;

@end

@implementation HydroRepairController

static NSString * headerID = @"headerID";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"水电报修"];
    self.eletricsArray = [[NSMutableArray alloc]init];
    
    [self initTableViewWithView:self.view frame:CGRectMake(0, 44, BHscreenW, BHscreenH - 44 - 64) isGroup:NO];
    self.tableView.showsVerticalScrollIndicator = NO;

    if (self.eletricsArray.count == 0) {
        
        UIView * lineView = [[UIView alloc]init];
        [self.tableView addSubview:lineView];
        lineView.backgroundColor = [BHColor(153, 153, 153)colorWithAlphaComponent:0.2];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.view).with.offset(0);
            make.top.equalTo(self.view).with.offset(44);
            make.size.mas_equalTo(CGSizeMake(BHscreenW, 15));
        }];

    }
    [self createPlaceHolder];
    
    [self createRefresh];
    
    //获取数据
    [self postEletricsRepairData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.eletricsArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HydroRepairCell * cell = [HydroRepairCell cellWithTableView:tableView];
    PublicRepairModel * model = self.eletricsArray[indexPath.section];
    cell.model = model;
    cell.orderCodeLabel.text = [NSString stringWithFormat:@"订单编号:%@",model.RepairCode];
    cell.repiarTypeLabel.text = model.CategoryName;
    cell.repiarAdressLabel.text = model.RepairAddr;
    cell.repiarManLabel.text = model.LinkMan;
    cell.appointTimeLabel.text = model.AppointTime;
    
    DLog(@"水电报修的状态 === %@",model.ProcessState);
    
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 245;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
  //  DLog(@"%ld===%ld",indexPath.section,indexPath.row);
    HydroRepairDetailController * hydroRepairDetVC = [[HydroRepairDetailController alloc]init];
    hydroRepairDetVC.model = self.eletricsArray[indexPath.section];
    [self.navigationController pushViewController:hydroRepairDetVC animated:YES];

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
    
}

#pragma mark - 暂未图
- (void)createPlaceHolder
{
    self.placehoderImg = [[UIImageView alloc]init];
    [self.tableView addSubview:self.placehoderImg];
    self.placehoderImg.image = [UIImage imageNamed:@"ico_repair_no"];
    
    [self.placehoderImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.tableView.mas_bottom).with.offset(100);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.tableView addSubview:self.titleLabel];
    self.titleLabel.text = @"暂无水电报修";
    
    self.titleLabel.textColor= BHColor(153, 153, 153);
    self.titleLabel.font = BHFont(15);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView).with.offset(0);
        make.top.equalTo(self.placehoderImg.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(100, 25));
    }];
    
}
#pragma mark - 创建刷新UI
- (void)createRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.nextPage = 1;
        
        self.isDown = YES;
        //获取数据
         [self postEletricsRepairData];
        
    }];
    
    //上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.nextPage += 1;
        
        self.isDown = NO;
        //获取数据
          [self postEletricsRepairData];
        
        //    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }];
}

#pragma mark - 获取数据
- (void)postEletricsRepairData{
    
    NSDictionary * parms = nil;
    parms = @{
              @"CommunityID":userModel.CommunityID,
              @"RepairType":@"20",
              @"Type":@"10",
              @"Page":[NSString stringWithFormat:@"%ld",self.nextPage],
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"repairlist\",\"version\":\"V1.0\"}"
              };
    
    [WorkOrderRequest postEletricsRepairSuccess:^(id data) {
        
        if (self.isDown == YES) {
            
            [self.eletricsArray removeAllObjects];
            self.isDown = NO;
        }
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"水电报修列表的数据 == %@",array);
        for (NSDictionary * dict in array) {
            
            PublicRepairModel * model = [[PublicRepairModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dict];
            
            [self.eletricsArray addObject:model];
            
          //  DLog(@"ProcessState == %@",model.ProcessState);
        }
        DLog(@" 显示的水电报修个数  == %ld",self.eletricsArray.count);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (self.eletricsArray.count != 0) {
            
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
        }else{
            self.tableView.mj_footer.hidden = YES;
    
        }
    
        
        if (self.eletricsArray.count <= 3) {
            
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView reloadData];
        
    } Fail:^(NSString *errorDes) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } param:parms];
    
}


@end
